package com.kady.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * Jdbc工具类
 *
 * @author 周凯迪
 * @date 2021-3-23
 */
public class JdbcUtil {

    private static final Properties PROPERTIES = new Properties();
    private static ThreadLocal<Connection> threadLocal = new ThreadLocal<>();
    private static Connection connection = null;
    //数据源
    private static DruidDataSource dataSource = null;

    static {
        InputStream in = null;
        try {
            in = JdbcUtil.class.getClassLoader().getResourceAsStream("com/kady/db.properties");
            PROPERTIES.load(in);

            dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(PROPERTIES);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != in) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 返回数据源
     * @return
     */
    public static DataSource getDataSource() {
        return dataSource;
    }


    /**
     * 获取连接
     *
     * @return
     */
    public static Connection getConnection() {
        try {

            //先从 ThreadLocal上获取绑定的线程
            connection = threadLocal.get();
            if (null == connection) {
                //如果线程上没有connection，获取一个
                connection = dataSource.getConnection();
                //把connection对象绑定到本地线程上
                threadLocal.set(connection);
            }

            System.out.println("---->" + connection);
            return connection;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 释放资源
     *
     * @param resultSet
     * @param statement
     * @param connection
     */
    public static void release(ResultSet resultSet, Statement statement, Connection connection) {
        try {
            if (null != resultSet) {
                resultSet.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (null != statement) {
                statement.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (null != connection) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    /**
     * 开启事务
     */
    public static void begin() {
        try {
            JdbcUtil.getConnection().setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 提交事务
     */
    public static void commit() {
        try {
            JdbcUtil.getConnection().commit();
            //从线程上解绑
            threadLocal.remove();
            //释放资源
            JdbcUtil.release(null, null,connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 回滚事务
     */
    public static void rollback() {
        try {
            JdbcUtil.getConnection().rollback();
            //从线程上解绑
            threadLocal.remove();
            //释放资源
            JdbcUtil.release(null, null,connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        Connection connection = getConnection();
        System.out.println(connection);
    }

}
